<?php

namespace app\controller;

use app\model\Activity;
use app\model\Cart;
use app\model\Catering;
use app\model\Hotel;
use app\model\Material;
use think\Request;

class Home extends Auth
{

    public function index()
    {
        return 'home';
    }

    public function addCart(Request $request)
    {
        $id       = $request->id;
        $type     = $request->type;
        $cart     = new Cart;
        $num      = 0;
        $checkNum = false;
        switch ($type) {
            case 'catering':
                $model = Catering::find($id);
                break;
            case 'activity':
                $model = Activity::find($id);
                break;
            case 'material':
                $num      = 1;
                $model    = Material::find($id);
                $checkNum = true;
                break;
            case 'hotel':
                $num      = 1;
                $model    = Hotel::find($id);
                $checkNum = true;
                break;
            default:
                $this->error('param error');
        }
        if ($checkNum) {
            if ($model->num < 1) {
                $this->result([], 1, 'num is not enough', 'json');
            }
        }
        $map = [
            'uid'  => get_uid(),
            'oid'  => $id,
            'type' => $type,
            'pic'  => $model->pic,
            'num'  => $num
        ];

        $cart->save($map);
        $this->result([], 1, 'add cart success', 'json');
    }

    public function carts()
    {
        $where = [
            'uid'    => get_uid(),
            'status' => 0
        ];
        $list  = Cart::where($where)->select()->toArray();
        foreach ($list as &$v) {
            switch ($v['type']) {
                case 'catering':
                    $model = Catering::find($v['oid']);
                    break;
                case 'activity':
                    $model = Activity::find($v['oid']);
                    break;
                case 'material':
                    $model = Material::find($v['oid']);
                    break;
                case 'hotel':
                    $model = Hotel::find($v['oid']);
                    break;
                default:
                    $this->error('param error');
            }
            $v['name'] = $model->name;
            $v['pic']  = $model->pic;
        }
        return view('carts', ['list' => $list]);
    }

    public function orders()
    {
        $where = [
            'uid'    => get_uid(),
            'status' => 1
        ];
        $list  = Cart::where($where)->select()->toArray();
        foreach ($list as &$v) {
            switch ($v['type']) {
                case 'catering':
                    $model = Catering::find($v['oid']);
                    break;
                case 'activity':
                    $model = Activity::find($v['oid']);
                    break;
                case 'material':
                    $model = Material::find($v['oid']);
                    break;
                case 'hotel':
                    $model = Hotel::find($v['oid']);
                    break;
                default:
                    $this->error('param error');
            }
            $v['name'] = $model->name;
            $v['pic']  = $model->pic;
        }
        return view('orders', ['list' => $list]);
    }

    public function pay(Request $request)
    {
        $id  = $request->id;
        $one = Cart::where(['uid' => get_uid(), 'id' => $id])->find();
        if (!$one) {
            $this->error('no access');
        }
        $checkNum = false;
        switch ($one->type) {
            case 'material':
                $model    = Material::find($one->oid);
                $checkNum = true;
                break;
            case 'hotel':
                $model    = Hotel::find($one->oid);
                $checkNum = true;
                break;
        }
        if ($checkNum) {
            $num = $one->num;
            if ($num < 1) {
                $this->error('');
                $this->result([], 0, 'num must > 0', 'json');
            }
            if ($num > $model->num) {
                $this->result([], 0, 'num is not enough', 'json');
            }
            if ($one->start == '0000:00:00' || $one->end == '0000:00:00') {
                $this->result([], 0, 'date is wrong', 'json');
            }
            $model->num -= $num;
            $model->save();
        }
        $one->status = 1;
        $one->save();

        $this->result([], 1, 'pay success', 'json');
    }

    public function upCart(Request $request)
    {
        $id  = $request->id;
        $one = Cart::where(['uid' => get_uid(), 'id' => $id])->find();
        if (!$one) {
            $this->error('no access');
        }
        return view('upcart', ['model' => $one]);
    }

    public function postCart(Request $request)
    {
        $id  = $request->id;
        $num = $request->num;
        $one = Cart::where(['uid' => get_uid(), 'id' => $id])->find();
        if (!$one) {
            $this->error('no access');
        }
        switch ($one->type) {
            case 'material':
                $model = Material::find($one->oid);
                break;
            case 'hotel':
                $model = Hotel::find($one->oid);
                break;
            default:
                $this->error('param error');
        }
        if ($num < 1) {
            $this->error('num must > 0');
        }
        if ($num > $model->num) {
            $this->error('num is not enough');
        }
        $one->num   = $num;
        $one->start = $request->start;
        $one->end   = $request->end;
        $one->save();
        $this->redirect('home/carts');

    }
}
